﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Word_Learner_3.Model;
using Telerik.WinControls.UI;
using Word_Learner_3.Web;
using System.Net;
using System.IO;

namespace Word_Learner_3
{
    public partial class Manager : Form
    {




        clsMCI cm;
        private int FloatingText_Top;
        private int FloatingText_Left;

        FloatingText w_FloatingText;
        Property w_Property;
        List<Word> w_WordList;
        private int w_WordPosition = 0;
        private int w_Interval;
        private bool w_ReadOrNot;
        List<Meta> metaList = null;


        public void LoadProperty()
        {
            Config config = SerializationHelper.Load(typeof(Config), Application.StartupPath + "\\Configuration.config") as Config;
            w_Interval = config.ConfigItem[0].Interval;
            this.FloatingText_Top = config.ConfigItem[0].Top;
            this.FloatingText_Left = config.ConfigItem[0].Left;
            timer1.Interval = w_Interval;
            w_ReadOrNot = config.ConfigItem[0].ReadOrNot;
        }

        public Manager()
        {
            InitializeComponent();
            LoadProperty();
            w_FloatingText = new FloatingText(FloatingText_Top, FloatingText_Left);
            w_FloatingText.Hide();
            AddPages();

        }
        public void ShowFloatingText(string str1, string str2, string str3)
        {
            WebClient webClient = new WebClient();
            //if (webClient.IsBusy)//是否存在正在进行中的Web请求
            //{
            //    webClient.CancelAsync();
            //}
            ////为webClient添加事件


            //webClient.DownloadFileAsync(new Uri("http://translate.google.com/translate_tts?q=" + str1), Application.StartupPath + "\\1.mp3");
            //webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
            //WebRequest wreq = WebRequest.Create("http://translate.google.com/translate_tts?q=" + str1);
            //WebResponse wresp = wreq.GetResponse();
            //Stream respStream = wresp.GetResponseStream();
            //int length = (int)wresp.ContentLength;
            //BinaryReader br = new BinaryReader(respStream);
            //FileStream fs;
            //fs = File.Create(Application.StartupPath + "\\1.mp3");
            //fs.Write(br.ReadBytes(length), 0, length);

            //br.Close();
            //fs.Close();
            try
            {
                if (w_ReadOrNot)
                {
                    try
                    {
                        cm.StopT();
                    }
                    catch
                    {
                    }

                    webClient.DownloadFile("http://translate.google.com/translate_tts?q=" + str1, Application.StartupPath + "\\play.mp3");
                    cm = new clsMCI();
                    cm.FileName = Application.StartupPath + "\\play.mp3";
                    cm.play();

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);w_ReadOrNot = false;
                
            }
            finally
            {
                
            }
            w_FloatingText.Change(str1, str2, str3);

        }
        private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {





        }
        public void AddPages()
        {
            radPageView1.Pages.Clear();
            metaList = new Meta().getMetas();
            foreach (Meta meta in metaList)
            {
                RadPageViewPage page = new RadPageViewPage();

                page.Text = meta.Name + "(" + meta.WordCount.ToString() + "个)";
                page.Name = meta.Key;
                // 
                // radGridView1
                // 
                RadGridView radGridView1 = new RadGridView();
                ((System.ComponentModel.ISupportInitialize)(radGridView1)).BeginInit();
                radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
                radGridView1.Location = new System.Drawing.Point(0, 0);
                radGridView1.Name = "radGridView1";
                radGridView1.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1);
                radGridView1.RootElement.Padding = new System.Windows.Forms.Padding(0, 0, 0, 1);
                radGridView1.Size = new System.Drawing.Size(271, 225);
                radGridView1.TabIndex = 0;
                radGridView1.ReadOnly = true;
                radGridView1.MasterTemplate.ShowColumnHeaders = false;
                radGridView1.MasterTemplate.ShowRowHeaderColumn = false;
                //radGridView1.MasterTemplate.AllowColumnHeaderContextMenu = false;
                radGridView1.ShowGroupPanel = false;
                radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                //radGridView1.EnableTheming=false;
                ((System.ComponentModel.ISupportInitialize)(radGridView1)).EndInit();
                // 
                
                // 
                //载入数据
                radGridView1.DataSource = DBHelper.ExecuteDataset("select Key,Phono,Trans,Example from " + page.Name).Tables[0].DefaultView;

                //radGridView1.Columns["Key"].HeaderText = "单词";
                //radGridView1.Columns["Phono"].HeaderText = "发音";
                //radGridView1.Columns["Trans"].HeaderText = "释义";
                //radGridView1.Columns["Example"].HeaderText = "例句";

                page.Controls.Add(radGridView1);
                radPageView1.Pages.Add(page);
            }

        }
        private void Manager_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            notify.ShowBalloonTip(2000);

            //startLesson();

        }
        private void startLesson()
        {

            w_WordPosition = 0;
            try
            {
                w_FloatingText.Close();
            }
            catch (Exception ex)
            {


            }

            timer1.Enabled = false;
            w_WordList = new Word(radPageView1.SelectedPage.Name).getWords();
            LoadProperty();
            w_FloatingText = new FloatingText(FloatingText_Top, FloatingText_Left);
            //w_FloatingText.loadPropertyEvent += new loadProperty(w_FloatingText.LoadProperty);
            w_FloatingText.Show();
            timer1.Enabled = true;


            ShowFloatingText(w_WordList[0].Key, w_WordList[0].Phono, w_WordList[0].Trans);



        }

        private void start_Click(object sender, EventArgs e)
        {
            startLesson();
        }


        private void stop_Click(object sender, EventArgs e)
        {

            try
            {
                w_FloatingText.Close();
            }
            catch (Exception ex)
            {

                throw;
            }




            timer1.Enabled = false;

        }


        private void button3_Click(object sender, EventArgs e)
        {

            w_Property = new Property(this, w_FloatingText);
            //w_Property.loadPropertyEvent += new loadProperty(LoadProperty);
            w_Property.Show();

        }
        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("小华编写", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void button5_Click(object sender, EventArgs e)
        {
            WebClient webClient = new WebClient();
            try
            {
                cm.StopT();
            }
            catch
            {
            }

            webClient.DownloadFile("http://translate.google.com/translate_tts?q=" + w_WordList[0].Example, Application.StartupPath + "\\play1.mp3");
            cm = new clsMCI();
            cm.FileName = Application.StartupPath + "\\play.mp3";
            cm.play();

            
        }
        private void button6_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (w_WordPosition < w_WordList.Count - 2)
                w_WordPosition++;
            else w_WordPosition = 0;
            ShowFloatingText(w_WordList[w_WordPosition].Key, w_WordList[w_WordPosition].Phono, w_WordList[w_WordPosition].Trans);

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            w_Property = new Property(this, w_FloatingText);
            //w_Property.loadPropertyEvent += new loadProperty(LoadProperty);
            w_Property.Show();
        }

       




    }
}
